package cn.chinasyq.photoquan.user.fragment;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import cn.chinasyq.photoquan.R;
import cn.chinasyq.photoquan.user.activity.EditNickNameActivity;
import cn.chinasyq.photoquan.user.activity.LoginActivity;
import cn.chinasyq.photoquan.user.bean.UserInfo;
import cn.chinasyq.photoquan.user.mode.UserMode;
import cn.chinasyq.photoquan.util.DialogUtil;
import cn.chinasyq.photoquan.view.ItemButtonDialog;
import cn.master.util.utils.ToastUtil;
import cn.master.volley.models.response.handler.ResponseHandler;

/**
 * 用户基本信息
 */
public class UserBasicInfoFragment extends Fragment implements
        OnClickListener, ResponseHandler.OnNeedLoginListener, ResponseHandler.OnSucceedListener,
        ResponseHandler.OnFailedListener {

    @SuppressWarnings("unused")
    private static final String TAG = UserBasicInfoFragment.class.getName();
    private static final String TAG_GENDER = "gender";

    private TextView tvNickName, tvSex, tvBirthday = null;
    private RelativeLayout rlytNickName, rlytBirthday, rlytSex = null;

    private View view = null;

    private Dialog waitForDialog = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_user_basic_info, container,
                false);
        initComponent();
        return view;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        waitForDialog = DialogUtil.createProgressDialog(getActivity());

    }

    @Override
    public void onResume() {
        loadData();
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    /**
     * 初始化组件
     */
    private void initComponent() {
        tvNickName = (TextView) view.findViewById(R.id.tv_nick_name);
        tvSex = (TextView) view.findViewById(R.id.tv_sex);

        rlytNickName = (RelativeLayout) view.findViewById(R.id.rlyt_nick_name);
        rlytSex = (RelativeLayout) view.findViewById(R.id.rlyt_sex);

        rlytNickName.setOnClickListener(this);
        rlytSex.setOnClickListener(this);
    }

    /**
     * 加载数据
     */
    private void loadData() {
        UserInfo uBean = UserMode.getCurrentUserInfo(getActivity());
        tvNickName.setText(uBean.getName());
        tvSex.setText(uBean.getGender());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rlyt_nick_name:
                updateNickName();
                break;
            case R.id.rlyt_sex:
                showSelSex();
                break;
            default:
                break;
        }
    }

    /**
     * 选择性别
     */
    private void showSelSex() {
        String[] sexs = new String[]{getString(R.string.male),
                getString(R.string.female)};
        ItemButtonDialog dialog = new ItemButtonDialog(getActivity(), sexs);
        dialog.setOnItemClickListener(new ItemButtonDialog.OnItemClickListener() {
            @Override
            public void onItemClick(View v, String txt, int index) {
                switch (index) {
                    case 0:
                        updateGender(UserInfo.MALE);
                        break;
                    case 1:
                        updateGender(UserInfo.FEMALE);
                        break;
                    default:
                        break;
                }
            }
        });
        dialog.show();
    }


    /**
     * 修改昵称
     */
    private void updateNickName() {
        Intent intent = new Intent(getActivity(), EditNickNameActivity.class);
        startActivity(intent);
    }

    /**
     * 修改性别
     *
     * @param gender
     */
    String gender;

    private void updateGender(String gender) {
        waitForDialog.show();
        this.gender = gender;
        ResponseHandler handler = new ResponseHandler(TAG_GENDER);
        handler.setOnSucceedListener(this);
        handler.setOnFailedListener(this);
        handler.setOnNeedLoginListener(this);
        UserMode.updateAttr(getActivity(), handler, "gender", gender
        );
    }

    @Override
    public void onNeedLogin(String tag) {
        closeWaitForDialog();
        startActivity(new Intent(getActivity(), LoginActivity.class));
    }

    /**
     * 修改性别，生日调用该方法刷新数据
     */
    @Override
    public void onSucceed(String tag, boolean isCache, Object data) {
        if (getActivity() == null) {
            return;
        }
        UserMode.getCurrentUserInfo(getActivity()).setGender(gender.equals(UserInfo.FEMALE) ? getString(R.string.female) : getString(R.string.male));
        loadData();
        closeWaitForDialog();
    }

    @Override
    public void onFailed(String tag, ResponseHandler.FailedStatus status, int resultCode, String msg) {
        if (msg == null) {
            switch (status) {
                case DATA_ERROR:
                    ToastUtil.show(getActivity(), R.string.tip_data_error);
                    break;
                case NETWORK_ERROR:
                    ToastUtil.show(getActivity(), R.string.servererrortips);
                    break;
            }
        } else {
            ToastUtil.show(getActivity(), msg);
        }
        closeWaitForDialog();
    }

    private void closeWaitForDialog() {
        if (waitForDialog != null && waitForDialog.isShowing()) {
            waitForDialog.dismiss();
        }
    }

}
